package com.chenyue.cm.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.common.domain.JdConstantValues;
import com.chenyue.cm.common.service.CommonInfoService;
import com.chenyue.cm.exception.SysRuntimeException;
import com.chenyue.cm.match.dao.PublicAreaMapper;
import com.chenyue.cm.match.domain.PublicArea;
import com.chenyue.cm.match.vo.Address;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @author libingxing
 */
@Service
public class CommonInfoServiceImpl extends BaseServiceImpl<JdConstantValues> implements CommonInfoService {

    private final PublicAreaMapper publicAreaMapper;

    public CommonInfoServiceImpl(
            @Qualifier("publicAreaMapper") PublicAreaMapper publicAreaMapper
    ) {
        this.publicAreaMapper = publicAreaMapper;
    }

    @Override
    @Resource(name = "jdConstantValuesMapper")
    public void setBaseMapper(LcBaseMapper<JdConstantValues, Serializable> baseMapper) {
        // TODO Auto-generated method stub
        this.baseMapper = baseMapper;
    }

    /**
     * 获取公共信息
     *
     * @param key 关键字
     * @return 公共信息
     */
    @Override
    public String constantValues(String key) {
        final JdConstantValues one = lambdaQuery().eq(JdConstantValues::getConstantKey, key).one();
        return one == null ? "" : one.getConstantValue();
    }

    @Override
    public List<PublicArea> getArea(Integer parentId) {
        if (parentId != null && parentId > 0) {
            return publicAreaMapper.selectList(
                    new LambdaQueryWrapper<PublicArea>()
                            .eq(PublicArea::getParentId, parentId)
            );
        }
        return publicAreaMapper.selectList(
                new LambdaQueryWrapper<PublicArea>()
                        .eq(PublicArea::getParentId, 100000)
        );
    }

    @Override
    public List<PublicArea> getAreaByLevel(Integer level) {
        return publicAreaMapper.selectList(
                new LambdaQueryWrapper<PublicArea>()
                        .eq(PublicArea::getLevel, level)
        );
    }

    /**
     * 根据code获取城市信息
     *
     * @param code 市级/区域级 code
     * @return 地址list
     */
    @Override
    public Integer getCity(Integer code) {
        final PublicArea area = publicAreaMapper.selectOne(
                new LambdaQueryWrapper<PublicArea>()
                        .eq(PublicArea::getId, code)
        );
        if (area == null) {
            return null;
        }
        return area.getLevel() == 3 ? area.getParentId() : area.getId();
    }

    /**
     * 根据区域code 获取省市县
     *
     * @param code 区域code
     * @return address
     */
    @Override
    public Address getAddressByArea(Integer code) {
        final Address address = new Address();
        try {
            PublicArea area = publicAreaMapper.selectOne(
                    new LambdaQueryWrapper<PublicArea>()
                            .eq(PublicArea::getId, code)
            );
            if (area == null) {
                throw new SysRuntimeException("您所选的区域咱不支持");
            }
            PublicArea city;
            if (area.getLevel() == 3) {
                address.setArea(area.getId() + "");
                city = publicAreaMapper.selectOne(
                        new LambdaQueryWrapper<PublicArea>()
                                .eq(PublicArea::getId, area.getParentId())
                );
                address.setCity(city.getId() + "");
            } else {
                city = publicAreaMapper.selectOne(
                        new LambdaQueryWrapper<PublicArea>()
                                .eq(PublicArea::getId, area.getId())
                );
                address.setCity(city.getId() + "");
            }
            final PublicArea province = publicAreaMapper.selectOne(
                    new LambdaQueryWrapper<PublicArea>()
                            .eq(PublicArea::getId, city.getParentId())
            );
            address.setProvince(province.getId() + "");
        } catch (SysRuntimeException e) {
            throw new SysRuntimeException("您所选的区域咱不支持");
        }
        return address;
    }
}
